package com.project.systemfront.controller;


import com.project.systemfront.common.Result;
import com.project.systemfront.pojo.ForUser;
import com.project.systemfront.pojo.dto.UserDto;
import com.project.systemfront.service.IForUserService;
import com.project.systemfront.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;


@RestController
@RequestMapping("/system/ForUser")
public class ForUserController
{

    @Autowired
    private IForUserService forUserService;

    @GetMapping
    public Result<ForUser>  getUserInfo(){
        Map<String, Object> map = ThreadLocalUtil.get();
        Long id=Long.parseLong(map.get("id").toString());
        return Result.success(forUserService.selectForUserById(id));
    }

    @PostMapping("/update")
    public Result<String> updateUserInfo(@RequestBody UserDto userdto){
        Map<String, Object> map = ThreadLocalUtil.get();
        Long id=Long.parseLong(map.get("id").toString());
        ForUser forUser = new ForUser();

        if (userdto.getEmail() != null) {
            forUser.setEmail(userdto.getEmail());
        }
        if (userdto.getNickname() != null) {
            forUser.setNickname(userdto.getNickname());
        }
        if (userdto.getPhone() != null) {
            forUser.setPhone(userdto.getPhone());
        }
        if (userdto.getAvatar() != null) {
            forUser.setAvatar(userdto.getAvatar());
        }
        forUser.setUpdateTime(new Date());
        forUser.setId(id);
        forUserService.updateForUser(forUser);
        return Result.success("修改成功");
    }

}
